package com.jluzh10.learning.web.controller;

import com.jluzh10.learning.domain.Course;

import com.jluzh10.learning.domain.Teacher;
import com.jluzh10.learning.domain.User;
import com.jluzh10.learning.service.CourseClassifyService;
import com.jluzh10.learning.service.CourseService;
import com.jluzh10.learning.service.TeacherService;
import com.jluzh10.learning.utils.ResponseData;
import com.jluzh10.learning.utils.UploadUtil;
import com.jluzh10.learning.utils.qiniu.QiniuUtil;
import com.jluzh10.learning.web.converter.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.net.PortUnreachableException;
import java.util.List;
import java.util.Objects;

/**
 * @author yzg
 * @create 2019/5/20
 *
 * 课程相关控制器
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    private final CourseService courseService;

    @Autowired
    public CourseController(CourseService courseService) {
        this.courseService = courseService;

    }

    @RequestMapping(value = "/{courseId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findJson(@PathVariable(value = "courseId") int courseId) throws Exception {
        //查询课程详细信息
        Course course = courseService.selectOne(courseId);
        if (course!=null){

        }

        //更新课程浏览次数
        courseService.updateByIdSelective(new Course(course.getId(),course.getHits()+1));
        return new ResponseData().success().data(course);
    }


    /**
     * 查询某个课程的详细信息
     * @param id 课程id
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "info/{id}", method = RequestMethod.GET)
    public ModelAndView findById(ModelAndView modelAndView,
                                 @PathVariable(value = "id") Integer id) throws Exception {

        modelAndView.addObject("page","courses");
        modelAndView.addObject("courseId",id);

        //查询课程详细信息
        Course course = courseService.selectOne(id);
        if (course==null){
            //课程不存在
            modelAndView.setViewName("nofindcourse");
            return modelAndView;
        }
        //更新课程浏览次数
        courseService.updateByIdSelective(new Course(course.getId(),course.getHits()+1));

        modelAndView.setViewName("article");
        modelAndView.addObject("course",course);
        return modelAndView;
    }

    /**
     * 查询用户是否收藏课程
     * @param courseId 课程id
     * @return
     */
    @RequestMapping(value = "collect/{courseId}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData isCollect(@PathVariable(value = "courseId",required = false) Integer courseId,
                                  @LoginUser User user) throws Exception {
        if (user==null){//用户没有登录
            return new ResponseData().fail();
        }else if (courseService.isCollect(user.getId(),courseId)){
            //用户收藏了课程
            return new ResponseData().success();
        }
        //用户没有收藏该课程
        return new ResponseData().fail();
    }



    /**
     * 查询所有课程页面
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(ModelAndView modelAndView,Integer size) throws Exception{

        modelAndView.addObject("page","courses");
        modelAndView.setViewName("list");

        return modelAndView;
    }

    /**
     * 加载课程列表
     * @param limit 每页数量
     * @param offset 当前页
     * @param course 查询条件
     * @return 课程列表json
     * @param orderBy 排序条件
     * @param key 搜索字符串
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData find(Course course, Integer limit,
                             Integer offset, String orderBy,
                             String key) throws Exception{

        List<Course> courses = courseService.list(limit, offset, course, orderBy, key);
        return new ResponseData().success().data(courses);
    }

    /**
     *
     * @param course 课程信息
     * @param file 课程封面图片
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addCourse(@Validated Course course, MultipartFile file) throws Exception {
        //上传封面到七牛云返回key
        if (!file.isEmpty()){
            String s = QiniuUtil.uploadFile(file.getInputStream(),
                    UploadUtil.generateRandonFileName(Objects.requireNonNull(file.getOriginalFilename())));
            //储存封面key
            course.setPicture(s);
        }
        //保存课程
        courseService.addCourse(course);

        return new ResponseData().success().data(course);
    }
}
